#pragma once

#include "cmpcxx.h"
#include <iostream>
#include <vector>
#include <set>
#include <numeric>      // std::iota
#include <algorithm>    // std::sort, std::stable_sort
#include <sstream> // for ostringstream

namespace cmpcxx {

//https://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes
template <typename T>
std::vector<size_t> argsort(const std::vector<T> &v) {

	// initialize original index locations
	std::vector<size_t> idx(v.size());
	iota(idx.begin(), idx.end(), 0);

	// sort indexes based on comparing values in v
	// using std::stable_sort instead of std::sort
	// to avoid unnecessary index re-orderings
	// when v contains elements of equal values
	std::stable_sort(idx.begin(), idx.end(), [&v](size_t i1, size_t i2) {return v[i1] < v[i2];});

	return idx;
}

template <typename T>
std::string set2string(const std::set<T> set) {
	ostringstream out;
	int first = 1;
	for (T el : set) {
		if (first) {
			first = 0;
		} else {
			out << ", ";
		}
		out << el;
	}
	string str = out.str();
	return str;
}

} // namespace cmpcxx
